package com.king.wms.controller;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.king.wms.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.king.wms.pojo.Buyer;
import com.king.wms.pojo.Loc;
import com.king.wms.pojo.Orders;
import com.king.wms.pojo.Receipt;
import com.king.wms.pojo.Sku;
import com.king.wms.pojo.Storer;

@Controller
@RequestMapping(value = "/orders")
public class OrdersController {
	@Autowired
	private ReceiptService receiptService;
	@Autowired
	private OrdersService ordersService;
	
	@Autowired
	private LocService locService;
	
	@Autowired
	private SkuService skuService;
	
	@Autowired
	private BuyerService buyerService;

	@RequestMapping(value = "/delete/{orders_id}")
	public String delete(@PathVariable Integer orders_id) {
		ordersService.delete(orders_id);
		return "redirect:/orders/findAll";
	}


	@RequestMapping(value = "/insert")
	public String insert(Orders orders,
			HttpServletResponse response, HttpServletRequest request)
					throws ServletException, IOException {
		
		String message = ordersService.insert(orders);
		if( "success".equals(message) ){
			return "redirect:/orders/findAll";
		}else{
			
			request.setAttribute("message", message);
			
			return "error";
		}
	}
	

	@RequestMapping(value = "/findAll")
	public void findAll(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		
		//List<Orders> list_orders = ordersService.findAll();
		
		List<Sku> list_sku = skuService.findAll();
		List<Loc> list_loc = locService.findAll();

		List<Orders> list_out = ordersService.findOut();
		List<Orders> list_notout = ordersService.findNotOut();
		
		List<Buyer> list_buyer = buyerService.findAll();
		
		
		request.setAttribute("list_out", list_out);
		request.setAttribute("list_notout", list_notout);
		request.setAttribute("list_loc", list_loc);
		request.setAttribute("list_sku", list_sku);
		request.setAttribute("list_buyer", list_buyer);
		
		request.getRequestDispatcher("/jsp/orders/orders.jsp").forward(request,
				response);

	}
	@RequestMapping(value = "/findAll1")
	public void findAll1(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {


		List<Orders> list_out = ordersService.findOut();
		List<Receipt> list_notout = receiptService.findReceived();

		String s1 = "['进出货单统计',"+list_out.size()+","+list_notout.size()+"]";


		request.setAttribute("s1", s1);


		request.getRequestDispatcher("/jsp/orders/jinchuhuodan.jsp").forward(request,
				response);

	}
	
	@RequestMapping(value = "/findOrdersdetailByOrders_id/{orders_id}")
	public void findOrdersdetailByOrders_id(@PathVariable("orders_id") Integer orders_id,
			HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		
		Orders orders = ordersService.findReceiptByPrimaryKey(orders_id);
		
		Loc loc = locService.findLocByLoc(orders.getLoc_id());
		
		Sku sku = skuService.findSkuBySku_id(orders.getSku_id());
		
		Buyer buyer = buyerService.findBuyerByBuyer_id(orders.getBuyer_id());
		
		request.setAttribute("orders", orders);
		request.setAttribute("loc", loc);
		request.setAttribute("sku", sku);
		request.setAttribute("buyer", buyer);
		
		request.getRequestDispatcher("/jsp/orders/ordersDetails.jsp").forward(
				request, response);
		
	}
	
	@RequestMapping(value = "/updateStatus/{orders_id}")
	public String updateStatus(@PathVariable("orders_id") Integer orders_id,
			HttpServletRequest request, HttpServletResponse response)
					throws ServletException, IOException {
		
		ordersService.updateStatus(orders_id);
		
		return "redirect:/orders/findAll";
		
	}
	
	@RequestMapping("/toUpdateOrdersPage/{orders_id}")
	public void toUpdateOrdersPage(@PathVariable("orders_id") Integer orders_id,
			HttpServletResponse response, HttpServletRequest request)
			throws ServletException, IOException {
		
		Orders orders = ordersService.findReceiptByPrimaryKey(orders_id);
		
		List<Loc> list_loc = locService.findAll();
		List<Sku> list_sku = skuService.findAll();
		List<Buyer> list_buyer = buyerService.findAll();

		request.setAttribute("list_loc", list_loc);
		request.setAttribute("list_buyer", list_buyer);
		request.setAttribute("list_sku", list_sku);
		request.setAttribute("orders", orders);
		
		
		request.getRequestDispatcher("/jsp/orders/updateOrders.jsp").forward(request,
				response);

	}
	
	@RequestMapping(value = "/update")
	public String update(Orders orders,
			HttpServletRequest request, HttpServletResponse response)
					throws ServletException, IOException {
		
		
		
		String message = ordersService.update(orders);
		
		if( "success".equals(message) ){
			return "redirect:/orders/findAll";
		}else{
			
			request.setAttribute("message", message);
			
			return "error";
		}
		
	}

}
